import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage'

Vue.use(Vuex)

const USER_KEY = 'user_token'

export default new Vuex.Store({
    state: {
        // user: JSON.parse(window.localStorage.getItem('user'))
        user: getItem(USER_KEY),
        // 要缓存的页面
        cachePages: ['layoutIndex']
    },
    mutations: {
        setUser (state, data) {
            state.user = data
            // window.localStorage.setItem('user', JSON.stringify(data))
            setItem(USER_KEY, state.user)
        },
        // 添加缓存的页面
        addCachePages (state, pageName) {
            if (!state.cachePages.includes(pageName)) {
                state.cachePages.push(pageName)
            }
        },
        // 移除缓存的页面
        removeCachePages (state, pageName) {
            const index = state.cachePages.indexOf(pageName)
            if (index !== -1) {
                state.cachePages.splice(index, 1)
            }
        }
    },
    actions: {
    },
    modules: {
    }
})
